package com.example.lirisheng.springbootfinalexperiment.controller;


import com.example.lirisheng.springbootfinalexperiment.entity.PersonInformation;
import com.example.lirisheng.springbootfinalexperiment.entity.RecordTemperature;
import com.example.lirisheng.springbootfinalexperiment.entity.Users;
import com.example.lirisheng.springbootfinalexperiment.pojo.UserAndRoles;
import com.example.lirisheng.springbootfinalexperiment.repositories.PersonInformationRepository;
import com.example.lirisheng.springbootfinalexperiment.service.AdminService;
import com.example.lirisheng.springbootfinalexperiment.service.CheService;
import com.example.lirisheng.springbootfinalexperiment.service.ResService;
import com.example.lirisheng.springbootfinalexperiment.util.GeneralUtil;
import com.example.lirisheng.springbootfinalexperiment.util.RespBean;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.*;

@RestController
@RequestMapping("/admin")
public class AdminController {


    @Autowired
    PersonInformationRepository personInformationRepository;
    @Autowired
    CheService cheService;

    @Autowired
    ResService resService;

    @Autowired
    AdminService adminService;
    public final Log logger = LogFactory.getLog(AdminController.class);

    @GetMapping("/hello")
    public RespBean hello() {

       return RespBean.ok("hello访问成功");
    }

    /**===================管理员======================================**/

    @ApiOperation(value = "重置密码", notes = "重置管理员人的密码")
    @PostMapping("/resetAdminPassword")
    public RespBean resetAdminPassword(@NotNull @RequestParam("id")Integer resId,
                                                  @NotNull @RequestParam("oldPassword")String oldPassword,
                                                  @NotNull @RequestParam("newPassword")String newPassword, BindingResult result){
        if (logger.isDebugEnabled()) {
            logger.debug("entry resetAdminPassword ");
        }
        //验证申请入库表单是否合法
        RespBean respBean;
        if((respBean= GeneralUtil.validateError(result))!=null){
            return  respBean;
        }

       try{
           return  adminService.resetAdminPassword(resId,oldPassword,newPassword);
       }catch (Exception exception){
           System.out.println("message:"+exception.getMessage());
           System.out.println("cause:"+exception.getCause());
           return RespBean.error("密码更新失败");

       }
    }

    /**===================检查人员======================================**/
    @ApiOperation(value = "查询账号信息", notes = "查询检所有检查人员的账号信息")
    @GetMapping("getAllChe")
    public RespBean  getChe(){

        if (logger.isDebugEnabled()) {
            logger.debug("entry getCheInfornation ");
        }
        List<Users>  users= cheService.getChe();

        return RespBean.ok("检查人员账号信息",users);
        //获取所有检查人员的账号信息
    }

    @ApiOperation(value = "添加检查人员账号", notes = "添加检查人员账号")
    @PostMapping("/addChe")
    public RespBean addChe(@NotNull @RequestParam("username")String username,
                           @NotNull @RequestParam("password")String password){
        if (logger.isDebugEnabled()) {
            logger.debug("entry addChe ");
        }



        Users users = new Users();
        users.setPassword(password);
        users.setUsername(username);
        try{
            cheService.addChe(users);
            return RespBean.ok("添加成功");
        }catch(Exception exception){
            System.out.println("message:"+exception.getCause());
            return RespBean.error("添加失败");
        }
    }

    /**===================住户===========================================**/
    @ApiOperation(value = "获得所有住户的账号和权限信息", notes = "获得所有住户的账号和权限信息")
    @GetMapping("/geAllUser")
    public RespBean geAllUser(){
        Map<String, Object> maps = new HashMap<String, Object>();
        if (logger.isDebugEnabled()) {
            logger.debug("entry  getResAccoutInfo");
        }
        List<UserAndRoles> userAndRoles= resService.getUserAccountInfo();
        return RespBean.ok("查询成功",userAndRoles);
    }

    @ApiOperation(value = "姓名查询住户的个人信息", notes = "姓名查询住户的个人信息")
    @GetMapping("/findUserInfoByName")
    public RespBean findUserInfoByName(@NotNull @RequestParam("name")String name){

        if (logger.isDebugEnabled()) {
            logger.debug("entry  findResInfoByName");
        }

        List<PersonInformation> personInformations=resService.findInfoByName(name);
        return RespBean.ok("查询成功",personInformations);
    }

    @ApiOperation(value = "获取住户的温度信息", notes = "res_id外键获取温度记录表信息")
    @PostMapping("findTempByUserId")
    public  RespBean findTempByUserId(@NotNull @RequestParam("restId")Long resId){

        if (logger.isDebugEnabled()) {
            logger.debug("entry  findResInfoByName");
            logger.debug("resId:"+resId);
        }
        List<RecordTemperature> recordTemperatures = resService.findTempByUserId(resId);
        return  RespBean.ok("查询成功",recordTemperatures);
    }


    @ApiOperation(value = "获取所有住户温度信息", notes = "获取所有住户温度信息")
    @GetMapping("/getAllTemp")
    public RespBean getAllTemp(){

        if (logger.isDebugEnabled()) {
            logger.debug("entry  getAllResTemp");
        }
        List<RecordTemperature> recordTemperatures = resService.findAllTemp();
        return  RespBean.ok("查询成功",recordTemperatures);
    }


    @ApiOperation(value = "通过账号id查找住户信息", notes = "通过账号id查找住户信息")
    @GetMapping("/getInfoByUserId")
    public RespBean getInfoByUserId(@NotNull @RequestParam("id") Long resId){

        if (logger.isDebugEnabled()) {
            logger.debug("entry  getInfoByResId");
            logger.debug("resId:"+resId);
        }
        PersonInformation personInformation = resService.getInfoByUserId(resId);
        return  RespBean.ok("查询成功",personInformation);
    }


    @ApiOperation(value = "锁定账号", notes = "锁定账号(检查人员或者住户)")
    @PostMapping("/lockAccount")
    public RespBean lockAccount(@NotNull @RequestParam("id")Long id){

        if (logger.isDebugEnabled()) {
            logger.debug("entry  lockAccount");
            logger.debug("id:"+id);
        }

        try {
            cheService.lockAccount(id);
            return RespBean.ok("锁定成功");
        }catch(Exception exception){
            System.out.println("message:"+exception.getMessage());
            return RespBean.error("锁定失败");

        }

    }


    @ApiOperation(value = "解锁账号", notes = "通过账号id查找解锁账号")
    @PostMapping("/unlockAccount")
    public RespBean unlockAccount(@NotNull @RequestParam("id") Long id){

        if (logger.isDebugEnabled()) {
            logger.debug("entry unlockAccount ");
            logger.debug("id:"+id);
        }
        try{
            cheService.unLockAccount(id);
            return RespBean.ok("解锁成功");
        }catch(Exception exception){
            System.out.println("message:"+exception.getMessage());
            return RespBean.error("解锁失败");
        }

    }


    @ApiOperation(value = "删除账号", notes = "通过账号id删除账号")
    @DeleteMapping("deleteAccount")
    public  RespBean deleteAccount(@NotNull @RequestParam("id") Long resId){
        if (logger.isDebugEnabled()) {
            logger.debug("entry deleteAccount ");
            logger.debug("resid:"+resId);
        }

        try{
            adminService.deleteAccount(resId);
            return RespBean.ok("删除成功");
        }catch (Exception exception){
            System.out.println("message:"+exception.getMessage());
            return RespBean.error("删除失败");
        }

    }

    @ApiOperation(value = "获得某一时间段的预警人数和高危人数", notes = "获得某一时间段的预警人数和高危人数")
    @GetMapping("/getWarn")
    public RespBean  getWarn(){
        if (logger.isDebugEnabled()) {
            logger.debug("entry  getWarn");
        }
        Map<String,Object> map = adminService.getWarn();
        return RespBean.ok("查询成功",map);
    }


    @ApiOperation(value = "获取所有住户的个人信息", notes = "获取所有住户的个人信息")
    @GetMapping("getUserPersonInfo")
    public RespBean getUserPersonInfo(){
        if (logger.isDebugEnabled()) {
            logger.debug("entry  getResPersonInfo");
        }
        List<PersonInformation> personInformations = adminService.getResPersonInfo();
        return RespBean.ok("查询成功",personInformations);
    }


}
